package com.thm.tfast.modules.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thm.tfast.core.entity.ViewResult;
import com.thm.tfast.modules.sys.model.SysMenu;
import com.thm.tfast.modules.sys.model.SysOrganization;
import com.thm.tfast.modules.sys.model.dto.menu.MenuInsertDto;
import com.thm.tfast.modules.sys.model.dto.menu.MenuUpdateDto;
import com.thm.tfast.modules.sys.model.dto.organization.OrganizationInsertDto;
import com.thm.tfast.modules.sys.model.dto.organization.OrganizationUpdateDto;
import com.thm.tfast.modules.sys.service.SysOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 组织机构信息 前端控制器
 * </p>
 *
 * @author THM
 * @since 2022-10-10
 */
@RestController
@Api(tags = "系统：组织管理")
@RequestMapping("/sysOrganization")
public class SysOrganizationController {

    @Resource
    private SysOrganizationService sysOrganizationService;

    @ApiOperation("根据条件分页获取组织机构列表")
    @GetMapping(value = "/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字：组织机构名称"),
            @ApiImplicitParam(name = "parentId", value = "父级ID"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true)
    })
    public ViewResult<Page<SysOrganization>> page(@RequestParam(value = "keyword", required = false) String keyword,
                                                  @RequestParam(value = "parentId", required = false) Long parentId,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysOrganization> page = sysOrganizationService.page(keyword, parentId, pageNum, pageSize);
        return ViewResult.success(page);
    }

    @ApiOperation("获取指定组织机构信息")
    @GetMapping("/{orgId}")
    public ViewResult<SysOrganization> getMenuById(@PathVariable Long orgId) {
        SysOrganization sysOrganization = sysOrganizationService.getById(orgId);
        return ViewResult.success(sysOrganization);
    }

    @ApiOperation("新增组织机构信息")
    @PostMapping("/insert")
    public ViewResult<Long> insert(@Validated @RequestBody OrganizationInsertDto dto){
        return ViewResult.success(sysOrganizationService.insert(dto));
    }

    @ApiOperation("修改组织机构信息")
    @PutMapping("/update")
    public ViewResult<String> update(@Validated @RequestBody OrganizationUpdateDto dto) {
        boolean success = sysOrganizationService.update(dto);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("删除组织机构信息")
    @DeleteMapping("/delete/{orgId}")
    public ViewResult<String> delete(@PathVariable Long orgId){
        boolean success = sysOrganizationService.delete(orgId);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }
}

